home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet multimedia / Animacje, filmy i prezentacje / Modelowanie 3D / K-3D 0.6.5.0 / k3d-all-in-one-setup-0.6.5.0.exe / k3d-setup-0.6.5.0.exe / share / shaders / k3d_material.h < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-19  |  7.0 KB  |  219 lines

  1. /************************************************************************
  2.  * material.h - Functions which compute the light response of materials.
  3.  *
  4.  * Author: Larry Gritz (gritzl@acm.org)
  5.  *
  6.  * Reference:
  7.  *   _Advanced RenderMan: Creating CGI for Motion Picture_, 
  8.  *   by Anthony A. Apodaca and Larry Gritz, Morgan Kaufmann, 1999.
  9.  *
  10.  * $Revision: 1.1 $    $Date: 2004/05/19 18:15:19 $
  11.  *
  12.  ************************************************************************/
  13.  
  14. #ifndef MATERIAL_H
  15. #define MATERIAL_H
  16.  
  17.  
  18. #include "k3d_locillum.h"
  19. #include "k3d_reflections.h"
  20.  
  21.  
  22.  
  23. /* Compute the color of the surface using a simple plastic-like BRDF.
  24.  * Typical values are Ka=1, Kd=0.8, Ks=0.5, roughness=0.1.
  25.  */
  26. color MaterialPlastic (normal Nf;  color basecolor;
  27.                        float Ka, Kd, Ks, roughness;)
  28. {
  29.     extern vector I;
  30.     return basecolor * (Ka*ambient() + Kd*diffuse(Nf))
  31.            + Ks*specular(Nf,-normalize(I),roughness);
  32. }
  33.  
  34.  
  35. /* Compute the color of the surface using a simple Lambertian BRDF. */
  36. color MaterialMatte (normal Nf;  color basecolor;  float Ka, Kd;)
  37. {
  38.     return basecolor * (Ka*ambient() + Kd*diffuse(Nf));
  39. }
  40.  
  41.  
  42. /* Compute the color of the surface using a simple metal-like BRDF.  To
  43.  * give a metallic appearance, both diffuse and specular components are 
  44.  * scaled by the color of the metal.  It is recommended that Kd < 0.1,
  45.  * Ks > 0.5, and roughness > 0.15 to give a believable metallic appearance.
  46.  */
  47. color MaterialRoughMetal (normal Nf;  color basecolor;
  48.                           float Ka, Kd, Ks, roughness;)
  49. {
  50.     extern vector I;
  51.     return basecolor * (Ka*ambient() + Kd*diffuse(Nf) +
  52.                         Ks*specular(Nf,-normalize(I),roughness));
  53. }
  54.  
  55.  
  56. /* Compute the color of the surface using a simple thin plastic-like BRDF.
  57.  * We call it _thin_ because it includes a transmisison component to allow 
  58.  * light from the _back_ of the surface to affect the appearance.  Typical
  59.  * values are Ka=1, Kd=0.8, Kt = 0.2, Ks=0.5, roughness=0.1.
  60.  */
  61. color MaterialThinPlastic (normal Nf;  vector V;  color basecolor; 
  62.                            float Ka, Kd, Kt, Ks, roughness;)
  63. {
  64.     return basecolor * (Ka*ambient() + Kd*diffuse(Nf) + Kt*diffuse(-Nf))
  65.            + Ks*specular(Nf,V,roughness);
  66. }
  67.  
  68.  
  69.  
  70. /* Compute the color of the surface using a simple plastic-like BRDF, with
  71.  * fresnel-attenuated coherent reflections.
  72.  * If twosided is nonzero, both sides are shiny, but if you know that
  73.  * the object has N facing outward, twosided==0 is a good optimization
  74.  * (especially when using the ray server).
  75.  */
  76. color
  77. MaterialShinyPlastic (normal Nf; color basecolor;
  78.                       float Ka, Kd, Ks, roughness, Kr, blur, ior;
  79.               uniform float twosided;
  80.                       DECLARE_ENVPARAMS; )
  81. {
  82.     extern vector I;
  83.     extern point P;
  84.     extern normal N;
  85.     vector IN = normalize(I), V = -IN;
  86.     float fkr, fkt;  vector R, T;
  87.     fresnel (IN, Nf, 1/ior, fkr, fkt, R, T);
  88.     fkt = 1-fkr;
  89.     if (twosided == 0 && N.I > 0)
  90.     fkr = 0;
  91.     return  fkt * basecolor * (Ka*ambient() + Kd*diffuse(Nf))
  92.           + (Ks) * specular(Nf,V,roughness)
  93.       + SampleEnvironment (P, R, fkr*Kr, blur, ENVPARAMS);
  94. }
  95.  
  96.  
  97.  
  98. /* Compute the color of the surface using a simple metal-like BRDF.  To
  99.  * give a metallic appearance, both diffuse and specular components are 
  100.  * scaled by the color of the metal.  It is recommended that Kd < 0.1,
  101.  * Ks > 0.5, and roughness > 0.15 to give a believable metallic appearance.
  102.  * If twosided is nonzero, both sides are shiny, but if you know that
  103.  * the object has N facing outward, twosided==0 is a good optimization
  104.  * (especially when using the ray server).
  105.  */
  106. color MaterialShinyMetal (normal Nf;  color basecolor;
  107.                           float Ka, Kd, Ks, roughness, Kr, blur;
  108.               uniform float twosided;
  109.                           DECLARE_ENVPARAMS;)
  110. {
  111.     extern point P;
  112.     extern vector I;
  113.     extern normal N;
  114.     float kr = Kr;
  115.     if (twosided == 0 && N.I > 0)
  116.     kr = 0;
  117.     vector IN = normalize(I), V = -IN;
  118.     vector R = reflect (IN, Nf);
  119.     return basecolor * (Ka*ambient() + Kd*diffuse(Nf) +
  120.                         Ks*specular(Nf,V,roughness) + 
  121.                         SampleEnvironment (P, R, kr, blur, ENVPARAMS));
  122. }
  123.  
  124.  
  125.  
  126. /* Compute the color of the surface of a very rough, totally nonspecular
  127.  * material like clay.  Use an Oren/Nayar BRDF for the diffuse term.
  128.  */
  129. color MaterialClay (normal Nf;  color basecolor;
  130.                     float Ka, Kd, roughness;)
  131. {
  132.     extern vector I;
  133.     return basecolor * (Ka*ambient() + 
  134.             Kd*LocIllumOrenNayar(Nf,-normalize(I),roughness));
  135. }
  136.  
  137.  
  138. /* Compute the color of the surface using an anisotropic BRDF.  To
  139.  * give a metallic appearance, both diffuse and specular components are 
  140.  * scaled by the color of the metal.  It is recommended that Kd < 0.1,
  141.  * Ks > 0.5, and roughness > 0.15 to give a believable metallic appearance.
  142.  */
  143. color MaterialBrushedMetal (normal Nf;  color basecolor;
  144.                             float Ka, Kd, Ks;
  145.                             vector xdir;  float uroughness, vroughness;)
  146. {
  147.     extern vector I;
  148.     color spec = LocIllumWardAnisotropic (Nf, -normalize(I), 
  149.                       xdir, uroughness, vroughness);
  150.     return basecolor * (Ka*ambient() + Kd*diffuse(Nf) + Ks*spec);
  151. }
  152.  
  153.  
  154. /* Compute the color of a ceramic object.  Like plastic, but use a
  155.  * "glossy" specular term.
  156.  */
  157. color MaterialCeramic (normal Nf;  color basecolor;
  158.                        float Ka, Kd, Ks, roughness, specsharpness;)
  159. {
  160.     extern vector I;
  161.     vector V = -normalize(I);
  162.     return basecolor * (Ka*ambient() + Kd*diffuse(Nf))
  163.               + Ks * LocIllumGlossy (Nf, V, roughness/10, specsharpness);
  164. }
  165.  
  166.  
  167.  
  168. /* Compute the color of a glass-like surface with coherent reflections
  169.  * and refractions.
  170.  */
  171. color MaterialGlass (normal Nf;  color basecolor;
  172.                      float Ka, Kd, Ks, roughness, Kr, reflblur;
  173.              float Kt, refrblur, eta;
  174.              color transmitcolor;
  175.              uniform float refrrayjitter, refrraysamples;
  176.              DECLARE_ENVPARAMS;)
  177. {
  178.     extern point P;
  179.     extern vector I;
  180.     extern normal N;
  181.  
  182.     vector IN = normalize (I);
  183.  
  184.     /* Compute the reflection & refraction directions and amounts */
  185.     vector Rfldir, Rfrdir;   /* Smooth reflection/refraction directions */
  186.     float kr, kt;
  187.     fresnel (IN, Nf, (I.N < 0) ? 1.0/eta : eta, kr, kt, Rfldir, Rfrdir);
  188.     kt = 1-kr;   /* Physically incorrect, but portable */
  189.     kr *= Kr;
  190.     kt *= Kt;
  191. #if (defined(BMRT) || defined(RAYSERVER_H))
  192.     /* Speedup -- at deep ray levels, reflection is unimportant */
  193.     if (raylevel() > 0)
  194.     kr = 0;
  195. #endif
  196. #ifndef BMRT
  197.     /* Speedup for PRMan -- don't shade back sides, but you HAVE to be sure
  198.      * that normals correctly face outward.
  199.      */
  200.     if (N.I > 0)
  201.     kr = kt = 0;
  202. #endif
  203.  
  204.     /* Calculate the reflection & refraction color */
  205.     color Crefl = SampleEnvironment (P, normalize(Rfldir), kr, reflblur,
  206.                      ENVPARAMS);
  207.     color Crefr = SampleEnvironment (P, normalize(Rfrdir), kt, refrblur,
  208.                      envname, envspace, envrad, 
  209.                      refrrayjitter, refrraysamples);
  210.     return (basecolor * (Ka*ambient() + Kd*diffuse(Nf)) +
  211.         (Crefl + Ks*LocIllumGlossy(Nf,-IN,roughness,0.5)) +
  212.         transmitcolor * Crefr);
  213. }
  214.  
  215.  
  216.  
  217. #endif /* defined(MATERIAL_H) */
  218.  
  219.